package com.WISSC.serviceImpl;

import com.WISSC.bean.Massage;
import com.WISSC.bean.Student;
import com.WISSC.bean.StudentImage;
import com.WISSC.mapper.StudentImageMapper;
import com.WISSC.service.StudentImageService;
import com.WISSC.util.file.FileUpload;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.*;

/**
 * <p>
 * 学员图片关联表 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-05-05
 */
@Service
public class StudentImageServiceImpl extends ServiceImpl<StudentImageMapper, StudentImage> implements StudentImageService {


    /**
     * @param file
     * @return 添加证书图片
     */
    @Override
    public Massage upload(MultipartFile file) {
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        StudentImage studentImage = new StudentImage();
        studentImage.setImageUrl(url);
        studentImage.setFileName(file.getOriginalFilename());
        studentImage.setCreateTime(new Date().getTime());
        Massage massage = new Massage();
        //上传成功 返回数据 返回本次上传的文件ID
        if (this.save(studentImage)) {
            massage.setCode(0);
            massage.setMsg("上传成功");
            Map map = new HashMap<>();
            map.put("fileId", studentImage.getId());
            map.put("url", url);
            massage.setData(map);
            return massage;
        }
        ;
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }

//    /**
//     * @return 获取对象
//     */
//    @Override
//    public List<StudentImage> getImageUrl(Map map) {
//        return baseMapper.getImageUrl(map);
//    }

    /**
     * @param studentId
     * @param file
     * @return 多图片编辑
     */
    @Override
    public Massage updatePicture(String studentId, MultipartFile file) {
        Long id = Long.parseLong(studentId);
        int i = baseMapper.remove(id);
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        StudentImage studentImage = new StudentImage();
        studentImage.setImageUrl(url);
        studentImage.setStudentId(Long.parseLong(studentId));
        studentImage.setCreateTime(new Date().getTime());
        Massage massage = new Massage();
        if (this.save(studentImage)) {
            massage.setCode(0);
            massage.setMsg("上传成功");
            return massage;
        }
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }

    @Override
    public List<StudentImage> getStudentImage(Map map) {
        return baseMapper.getStudentImage(map);
    }

    @Override
    public Massage updateimageUrl(MultipartFile file, String stuId) {
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Massage massage = new Massage();
        StudentImage studentImage = new StudentImage();
        studentImage.setId(Long.parseLong(stuId));
        studentImage.setImageUrl(url);
        if (baseMapper.updateById(studentImage) > 0) {
            massage.setCode(0);
            massage.setMsg("上传成功");
            return massage;
        }
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }

    @Override
    public boolean removeByStudentId(Long id) {
        if (baseMapper.remove(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<StudentImage> getBystuId(Long id) {
        return baseMapper.getBystuId(id);
    }


}
